package com.china08.yxyapi.api.manage.sch.platform.website.section;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSection;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSectionRepository;

@CrossOrigin
@RestController("SectionListController")
public class SectionListController extends YxyApiBaseController {

    @Autowired
    SwsSectionRepository swsSectionRepository;

    @RequestMapping(value = "/api_yw/manage/sch/platform/website/section/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, HttpServletResponse response) {
        // 当前用户学校
        String schoolId = getProfile(request, response).getSchoolId();

        List<SwsSection> swsSectionList = swsSectionRepository.findBySchoolIdAndPidOrderByOrderNumAsc(schoolId, "0");

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        // 获取二级
        for (SwsSection swsSection : swsSectionList) {
            String pid = swsSection.getId();
            List<SwsSection> swsSections2 = swsSectionRepository.findBySchoolIdAndPidOrderByOrderNumAsc(schoolId, pid);

            String sectionName = swsSection.getSectionName();
            boolean showNavigation = swsSection.getShowNavigation();
            boolean showInHomePage = swsSection.getShowInHomePage();
            Integer sectionType = swsSection.getSectionType();
            String displayStyle = swsSection.getDisplayStyle().toString();
            boolean isAudit = swsSection.getIsAudit();
            Integer orderNum = swsSection.getOrderNum();
            boolean showNavigationForMicroWebSite = swsSection.isShowNavigationForMicroWebSite();

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", pid);
            map.put("pid", "0");
            map.put("sectionName", sectionName);
            map.put("showNavigation", showNavigation);
            map.put("showInHomePage", showInHomePage);
            map.put("sectionType", sectionType);
            map.put("displayStyle", displayStyle);
            map.put("isAudit", isAudit);
            map.put("orderNum", orderNum);
            map.put("swsSections2", swsSections2);
            map.put("showNavigationForMicroWebSite", showNavigationForMicroWebSite);
            list.add(map);
        }
        RespModel respModel = new RespModel();
        respModel.list = list;
        return respModel;
    }

    protected static class RespModel extends YxyApiRespModel {
        public List<Map<String, Object>> list;
    }
}
